package com.kmx.demo.config;

import java.io.File;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.kmx.demo.utils.LoadProperties;

public class Configuration {

	private Properties properties;
	private String configPath = "conf/config.properties";

	public Configuration() {
		this.properties = new LoadProperties(configPath).load();
	}

	public Configuration(String configFilePath) {
		this.configPath = configFilePath;
		this.properties = new LoadProperties(configPath).load();
	}

	public Properties getProperties() {
		return properties;
	}

	public String getConfigPath() {
		return configPath;
	}

	public Object getProperty(String key) {
		return properties.get(key);
	}

	public String getOutputDir() {
		String outputDir = properties.getProperty("outputDir");
		return outputDir.endsWith(File.separator) ? outputDir : outputDir + File.separator;
	}

	public int getFgNum() {
		return Integer.parseInt(properties.getProperty("filegroupNum"));
	}

	public int getAssetsNumPerFg() {
		return Integer.parseInt(properties.getProperty("assetsNumPerFg"));
	}

	public String getStartTime() {
		return properties.getProperty("startTime");
	}

	public long getDuration() {
		String duration = properties.getProperty("duration");
		String unit = "x";
		Pattern p = Pattern.compile("(\\d+)");
		Matcher m = p.matcher(duration);
		long data = 0;
		if (m.find()) {
			data = Long.parseLong(m.group(1));
		}
		unit = duration.replace(data + "", "");
		switch (unit) {
		case "h":
			return 3600000 * data;
		case "m":
			return 60000 * data;
		case "d":
			return 3600000 * 24 * data;
		default:
			System.out.println("Duration's unit not in [m(minute),h(hour),d(day)], will use the default value 'm'");
			return 60000 * data;
		}

	}

	public long getDataFreq() {
		return Long.parseLong(properties.getProperty("dataFreq"));
	}

	public String getCsvTerminator() {
		return properties.getProperty("terminator");
	}
	
	
	public String getSdmIp() {
		return properties.getProperty("sdmServerIp");
	}

	
	public boolean isContainEx(){
		return Boolean.parseBoolean(properties.getProperty("isContainExData"));
		
	}
	
	public boolean isGenMetadata(){
		return Boolean.parseBoolean(properties.getProperty("genMetadata"));
		
	}
	
	public static void main(String[] args) {

		Pattern p = Pattern.compile("(\\d+)");
		String a = "1s23 m";
		Matcher m = p.matcher(a);
		if (m.find()) {
			System.out.println(m.group(1));
		}
	}
}
